package com.shirley.netty.ssl.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.LineEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.util.CharsetUtil;

import java.io.InputStream;

public class SSLTcpServerChannel extends ChannelInitializer {

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ClassLoader classLoader = this.getClass().getClassLoader();
        InputStream privateKeyIs = classLoader.getResourceAsStream("server#8.pem");
        InputStream x509certIs = classLoader.getResourceAsStream("server.crt");

        SslContext sslCtx = SslContextBuilder.forServer(x509certIs, privateKeyIs).build();

        ch.pipeline()
                .addLast(sslCtx.newHandler(ch.alloc()))
                .addLast(new LineBasedFrameDecoder(1024))
                .addLast(new StringDecoder())

                .addLast(new LineEncoder(CharsetUtil.UTF_8))

                .addLast(new SSLTcpServerInputMsgHandler());

    }
}
